package com.smec.moblie.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.smec.moblie.R;
import com.smec.moblie.adapter.ScenePhotoAdapter;
import com.smec.moblie.bean.MaintenancePhotoBean;
import com.smec.moblie.common.ConstValue;
import com.smec.moblie.control.EventCenter;
import com.smec.moblie.crash.ExitAppUtils;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * 显示场景照
 * Created by sunyubo on 16/3/9.
 */
public class DisplayScenePicturesActivity extends BaseActivity implements ConstValue {
    /**
     * 工单id
     **/
    private String workorderid;
    /**
     * 拍照项列表
     **/
    private ArrayList<MaintenancePhotoBean> maintenancePhotoBeanSceneList;
    /**
     * 返回按钮
     **/
    private ImageView imageview_back;
    /**
     * 场景照gridview
     **/
    private GridView gridView_Scene;
    /**
     * 场景照adapter
     **/
    private ScenePhotoAdapter scenePhotoAdapter;

    private RelativeLayout rl_title;

    private String wo_status_code;

    @Override
    protected void setContentView() {
        setContentView(R.layout.remes_act_displayscenepictures);
    }

    @Override
    protected void initView() {
        ExitAppUtils.getInstance().addActivity(this);

        rl_title = (RelativeLayout)findViewById(R.id.rl_title);
//        PublicControl.setMargins(rl_title, 0, getStatusBarHeight(DisplayScenePicturesActivity.this), 0, 0);

        workorderid = getIntent().getStringExtra("workorderid");
        wo_status_code = getIntent().getStringExtra("wo_status_code");

        imageview_back = (ImageView) findViewById(R.id.imageview_back);
        gridView_Scene = (GridView) findViewById(R.id.gridView_Scene);

        imageview_back.setOnClickListener(this);

        scenePhotoAdapter = new ScenePhotoAdapter(DisplayScenePicturesActivity.this);
    }

    @Override
    protected void getData() {
        maintenancePhotoBeanSceneList = (ArrayList<MaintenancePhotoBean>) DataSupport.where("workorder_id = ? AND object_status_code = 'VALID' AND item_type_code = 'ADDITIONAL_IMG'", workorderid).find(MaintenancePhotoBean.class);
        scenePhotoAdapter.setList(maintenancePhotoBeanSceneList);
        gridView_Scene.setAdapter(scenePhotoAdapter);
        gridView_Scene.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
                final MaintenancePhotoBean maintenancePhotoBean = maintenancePhotoBeanSceneList.get(position);
                if ("COMPLETED".equals(wo_status_code)) {
                    File file = new File(imgPath + maintenancePhotoBean.getResource_id() + ".jpeg");
                    File filetemp = imageLoader.getDiscCache().get(SERVER_BASE_PATH + "rest/resources/" + maintenancePhotoBean.getResource_id());
                    if (file.exists()) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(file), "image/*");
                        startActivity(intent);
                    } else if(filetemp.exists()){
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(filetemp), "image/*");
                        startActivity(intent);
                    }else {
                        Toast.makeText(DisplayScenePicturesActivity.this, "未获取到本地照片", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    new AlertView("提示", null, "取消", null, new String[]{"查看照片", "删除照片"}, DisplayScenePicturesActivity.this, AlertView.Style.ActionSheet, new OnItemClickListener() {
                        @Override
                        public void onItemClick(Object o, int position1) {
                            if (position1 == 0) {
                                File file = new File(imgPath + maintenancePhotoBean.getResource_id() + ".jpeg");
                                File filetemp = imageLoader.getDiscCache().get(SERVER_BASE_PATH + "rest/resources/" + maintenancePhotoBean.getResource_id());
                                if (file.exists()) {
                                    Intent intent = new Intent(Intent.ACTION_VIEW);
                                    intent.setDataAndType(Uri.fromFile(file), "image/*");
                                    startActivity(intent);
                                } else if(filetemp.exists()){
                                    Intent intent = new Intent(Intent.ACTION_VIEW);
                                    intent.setDataAndType(Uri.fromFile(filetemp), "image/*");
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(DisplayScenePicturesActivity.this, "未获取到本地照片", Toast.LENGTH_SHORT).show();
                                }
                            } else if (position1 == 1) {
                                maintenancePhotoBeanSceneList.remove(position);
                                scenePhotoAdapter.setList(maintenancePhotoBeanSceneList);
                                MaintenancePhotoBean maintenancePhotoBeanTemp = new MaintenancePhotoBean();
                                maintenancePhotoBeanTemp.setObject_status_code("INVALID");
                                maintenancePhotoBeanTemp.update(maintenancePhotoBean.getId());
                                EventBus.getDefault().post(new EventCenter.DeleteScenePhoto());
                            }
                        }
                    }).show();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回按钮
            case R.id.imageview_back:
                DisplayScenePicturesActivity.this.finish();
                break;
            default:
                break;
        }
    }
}
